package org.smi.sort;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Main m = new Main();
        List<Gjb> gjbList = new ArrayList<>();
        Gjb g1 = new Gjb(1, 1, "g1", false);
        Gjb g2 = new Gjb(2, 2, "g2", false);
        Gjb g3 = new Gjb(3, 2, "g3", true);
        Gjb g4 = new Gjb(4, 4, "g4", false);
        gjbList.add(g1);
        gjbList.add(g2);
        gjbList.add(g3);
        gjbList.add(g4);

        m.sort1(gjbList);
    }

    public void sort1(List<Gjb> gjbList) {

        for (Gjb s : gjbList) {
            s.getUid();
        }

        List<Gjb> modifyList = new ArrayList<>();
        List<Integer> sortValueList = new ArrayList<>();
        for (Gjb s : gjbList) {
            // 找到修改了sort的元素
            if (s.getModify()) {
                modifyList.add(s);
            } else {
                sortValueList.add(s.getSort());
            }
        }
        gjbList.removeAll(modifyList);
        for (Gjb s : modifyList) {
            if (sortValueList.contains(s.getSort())) {
                // 是否与没有重新排序的有冲突
                // 让原来list后移
                offset(gjbList, s.getSort());
            }
        }

        gjbList.addAll(modifyList);

        System.out.println("");
    }

    public void offset(List<Gjb> gjbList, Integer sort) {
        for (Gjb s : gjbList) {
            if (s.getSort() >= sort) {
                s.setSort(s.getSort() + 1);
            }
        }
    }
}
